//
//  ViewController.m
//  TVCloud
//
//  Created by Clément Raussin on 14/05/12.
//  Copyright (c) 2012 Clément Raussin. All rights reserved.
//

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "IIViewDeckController.h"

@interface ViewController ()

@property (nonatomic, strong) IIViewDeckController* containerController;

@end

@implementation ViewController


@synthesize containerView = _containerView;
@synthesize leftController = _leftController;
@synthesize centerController = _centerController;
@synthesize containerController = _containerController;


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    
    
    
    leftController = [[LeftController alloc] initWithNibName:@"LeftController" bundle:nil];
    centerController = [[CenterController alloc] initWithNibName:@"CenterController" bundle:nil];
    rightController = [[LeftController alloc] initWithNibName:@"LeftController" bundle:nil];
    
    self.containerController = [[IIViewDeckController alloc] initWithCenterViewController:centerController leftViewController:leftController rightViewController:rightController];
    self.containerController.panningMode = IIViewDeckFullViewPanning;
    //self.containerController.leftLedge = 700;
    self.containerController.leftLedge = 250;
    self.containerController.rightLedge = 250;
    self.containerController.elastic = YES;
    self.containerController.view.frame = self.containerView.bounds;
    [self.containerView addSubview:self.containerController.view];
    
    
    
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)controler:(id)sender{
    
    NSLog(@"controller");
    //[centerController2 center2]; 

}

- (IBAction)toggleLeft:(id)sender{
     NSLog(@"Toogle Left");
    [_containerController toggleLeftView];
}

- (IBAction)toggleRight:(id)sender{
    NSLog(@"Toogle Right");
    [_containerController toggleRightView];
}

- (IBAction)cloudTab:(id)sender{
    [centerController removeAllPages];
    [centerController addView:@"serie"];

}

- (IBAction)ajoutPage{
    [centerController addView:@"serie"];
    [self closeLeft];
    [self closeRight];
}


- (void)closeLeft{
    [_containerController closeLeftView];
}

- (void)closeRight{
    [_containerController closeLeftView];
}

- (void)sendToCenter{
    //[LeftController removeFromSuperview];
    NSLog(@"controller -> send to center");
    [centerController recieveCenter];
}

- (BOOL)changePage:(NSString *)args{    
     NSArray *components = [args componentsSeparatedByString:@":OO:"];
     if ([components count] > 1 && [[components objectAtIndex:0] isEqualToString:@"objc"]) {
         [self closeLeft]; 
         [self closeRight];
         
         [self downloadJSON:@"member/me/infos"];
         [centerController displayPage:components];
         return NO;
     }
    return YES;
    
}









//////////////////////////////////////////////////////////////
//////////////// GESTION DES REQUETES JSON ///////////////////
//////////////////////////////////////////////////////////////


NSString *appKey = @"Dwchsvq8QiPxTkxLQmAf";
NSString *memberToken = @"2jhh23L9e5DNv6aZOYHn";





- (void)downloadJSON:(NSString *)json{
    
    NSString *stringURL = [NSString stringWithFormat: @API_URL"%@.js?key=%@&token=%@", json, appKey, memberToken];
    NSURL *url = [NSURL URLWithString:stringURL];
    NSLog(@"Requete JSON %@",stringURL);
    NSString *mediaPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    
    if([json isEqualToString:@"serie"]){
    
    url = [NSURL URLWithString:@"http://tvcloud.fr/tempAPI/shows/serie.json"];
    mediaPath = [mediaPath stringByAppendingPathComponent:@"serie.js"];
    NSLog(@"Recherche du JSON Serie");
        
    }else {
        mediaPath = [mediaPath stringByAppendingPathComponent:@"cache.js"];
        NSLog(@"Recherche du JSON cache");
    }
    
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];


   
    
    
    [request setDownloadDestinationPath:mediaPath];
    [request setDelegate:self];
    [request startAsynchronous];

        
}


- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    WBNoticeView *nm = [WBNoticeView defaultManager];
    [nm showSuccessNoticeInView:self.view message:@"Requete HTTP effectué avec succès"];
    
    //NSString *responseString = [request responseString];
    
    // Use when fetching binary data
    //NSData *responseData = [request responseData];
    NSLog(@"Requete JSON executée avec succes");
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   // NSError *error = [request error];
    WBNoticeView *nm = [WBNoticeView defaultManager];
    [nm showErrorNoticeInView:self.view title:@"Erreur" message:@"Impossible de se connecter au serveur"];
    [centerController removeLastPage];
}


-(void)endLoading {
    [centerController endLoading];
}


@end
